package com.lottery.ruok.lottrey.chat.model.redpacket.message.sendRedPacketMessage;

import android.os.Parcel;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

@MessageTag(value = "RCD:SendRPMsg", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class RedPacketMessage extends MessageContent {
    private static final String TAG = "RedPacketMessage";
    public static final String MSG_ID = "msgId";
    public static final String MONEY = "money";
    public static final String CONTENT = "content";
    public static final String SEND_USER_ID = "sendUserId";
    public static final String RECEIVE_USER_ID = "receiveUserId";
    public static final String TARGET_ID = "targetId";
    public static final String USER = "user";
    public static final String EXTRA = "extra";
    public static final String TYPE = "type";
    public static final String DATE = "date";

    public static final String SEND_USER_NICK_NAME = "sendUserNickname";
    public static final String SEND_USER_AVATAR = "sendUserAvatar";
    public static final String RECEIVE_USER_NICK_NAME = "receiveUserNickname";
    public static final String RECEIVE_USER_AVATAR = "receiveUserAvatar";
    /**
     * msgId    红包id long
     * content   消息内容（如“恭喜发财“），可为空 string
     * extra    备注 string
     * type. 1:私人红包。2:群平均红包 3:群随机红包 int
     * money    红包金额 float
     * sendUserId 发送者id string
     * receiveUserId (私人红包)接收用户id, string
     * targetId 房间Id string
     * date 时间 date
     */
    private long msgId;
    private String content;
    private String extra;
    private int type;
    private double money;
    private String sendUserId;
    private String receiveUserId;
    private String targetId;
    private String date;
    private String sendUserNickname;
    private String sendUserAvatar;
    private String receiveUserNickname;
    private String receiveUserAvatar;

    public RedPacketMessage() {

    }

    public RedPacketMessage(long msgId, String content, String extra, int type, double money, String sendUserId, String receiveUserId, String targetId, String date,String sendUserNickname ,String sendUserAvatar ,String  receiveUserNickname,String receiveUserAvatar) {
        this.msgId = msgId;
        this.content = content;
        this.extra = extra;
        this.type = type;
        this.money = money;
        this.sendUserId = sendUserId;
        this.receiveUserId = receiveUserId;
        this.targetId = targetId;
        this.date = date;
        this.sendUserNickname=sendUserNickname;
        this.sendUserAvatar=sendUserAvatar;
        this.receiveUserNickname=receiveUserNickname;
        this.receiveUserAvatar=receiveUserAvatar;
    }

    public static final Creator<RedPacketMessage> CREATOR = new Creator<RedPacketMessage>() {
        @Override
        public RedPacketMessage createFromParcel(Parcel source) {
            return new RedPacketMessage(source);
        }

        @Override
        public RedPacketMessage[] newArray(int size) {
            return new RedPacketMessage[size];
        }
    };

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put(MSG_ID, getMsgId()); // 这里的id（联系人）不同于下边发送名片信息者的 sendUserId
            jsonObject.put(CONTENT, getContent());
            jsonObject.put(EXTRA, getExtra());
            jsonObject.put(TYPE, getType());
            jsonObject.put(MONEY, getMoney());
            jsonObject.put(SEND_USER_ID, getSendUserId());
            jsonObject.put(RECEIVE_USER_ID, getReceiveUserId());
            jsonObject.put(TARGET_ID, getTargetId());
            jsonObject.put(DATE, getDate());

            jsonObject.put(SEND_USER_NICK_NAME, getSendUserNickname());
            jsonObject.put(SEND_USER_AVATAR, getSendUserAvatar());
            jsonObject.put(RECEIVE_USER_NICK_NAME, getReceiveUserNickname());
            jsonObject.put(RECEIVE_USER_AVATAR, getReceiveUserAvatar());

            if (getJSONUserInfo() != null)
                jsonObject.putOpt(USER, getJSONUserInfo());
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

        try {
            return jsonObject.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public RedPacketMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            if (jsonObj.has(MSG_ID))
                setMsgId(jsonObj.optLong(MSG_ID));
            if (jsonObj.has(CONTENT))
                setContent(jsonObj.optString(CONTENT));
            if (jsonObj.has(EXTRA))
                setExtra(jsonObj.optString(EXTRA));
            if (jsonObj.has(TYPE))
                setType(jsonObj.optInt(TYPE));
            if (jsonObj.has(MONEY))
                setMoney(jsonObj.optDouble(MONEY));
            if (jsonObj.has(SEND_USER_ID))
                setSendUserId(jsonObj.optString(SEND_USER_ID));
            if (jsonObj.has(RECEIVE_USER_ID))
                setReceiveUserId(jsonObj.optString(RECEIVE_USER_ID));
            if (jsonObj.has(TARGET_ID))
                setTargetId(jsonObj.optString(TARGET_ID));
            if (jsonObj.has(DATE))
                setDate(jsonObj.optString(DATE));


            if (jsonObj.has(SEND_USER_NICK_NAME))
                setSendUserNickname(jsonObj.optString(SEND_USER_NICK_NAME));
            if (jsonObj.has(SEND_USER_AVATAR))
                setSendUserAvatar(jsonObj.optString(SEND_USER_AVATAR));
            if (jsonObj.has(RECEIVE_USER_NICK_NAME))
                setReceiveUserNickname(jsonObj.optString(RECEIVE_USER_NICK_NAME));
            if (jsonObj.has(RECEIVE_USER_AVATAR))
                setReceiveUserAvatar(jsonObj.optString(RECEIVE_USER_AVATAR));
            if (jsonObj.has(USER)) {
                setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));
            }
        } catch (JSONException e) {
            RLog.e(TAG, "JSONException " + e.getMessage());
        }
    }

    public RedPacketMessage(Parcel in) {

        msgId = in.readLong();
        content = in.readString();
        extra = in.readString();
        type = in.readInt();
        money = in.readDouble();
        sendUserId = in.readString();
        receiveUserId = in.readString();
        targetId = in.readString();
        date = in.readString();

        sendUserNickname=in.readString();
        sendUserAvatar=in.readString();
        receiveUserNickname=in.readString();
        receiveUserAvatar=in.readString();

        setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeLong(msgId);
        dest.writeString(content);
        dest.writeString(extra);
        dest.writeInt(type);
        dest.writeDouble(money);
        dest.writeString(sendUserId);
        dest.writeString(receiveUserId);
        dest.writeString(targetId);
        dest.writeString(date);

        dest.writeString(sendUserNickname);
        dest.writeString(sendUserAvatar);
        dest.writeString(receiveUserNickname);
        dest.writeString(receiveUserAvatar);
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    public long getMsgId() {
        return msgId;
    }

    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getSendUserId() {
        return sendUserId;
    }

    public void setSendUserId(String sendUserId) {
        this.sendUserId = sendUserId;
    }

    public String getReceiveUserId() {
        return receiveUserId;
    }

    public void setReceiveUserId(String receiveUserId) {
        this.receiveUserId = receiveUserId;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSendUserNickname() {
        return sendUserNickname;
    }

    public void setSendUserNickname(String sendUserNickname) {
        this.sendUserNickname = sendUserNickname;
    }

    public String getSendUserAvatar() {
        return sendUserAvatar;
    }

    public void setSendUserAvatar(String sendUserAvatar) {
        this.sendUserAvatar = sendUserAvatar;
    }

    public String getReceiveUserNickname() {
        return receiveUserNickname;
    }

    public void setReceiveUserNickname(String receiveUserNickname) {
        this.receiveUserNickname = receiveUserNickname;
    }

    public String getReceiveUserAvatar() {
        return receiveUserAvatar;
    }

    public void setReceiveUserAvatar(String receiveUserAvatar) {
        this.receiveUserAvatar = receiveUserAvatar;
    }
}
